﻿package net.libra.ui 
{
	import net.libra.core.interfaces.IDestroy;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * 页签组件。
	 * 对象负责处理页签组件中的按钮外观，当点击不同的页签时，处理每个页签不同的外观变化。
	 * 对象自带一个页签切换的处理器。当点击按钮时，显示该按钮相应的页面。也可以不设置页面切换器。
	 * @author DanielLu
	 */
	public class TabbedPane extends EventDispatcher implements IDestroy
	{
		public static const TAB_CHANGED : String = "tabChanged";

		private var content : DisplayObjectContainer;
		private var tabs : Array;
		private var selectedIndex : int;
		private var group : ButtonGroup;

		/**
		 * 页签组件。
		 * @param content	显示页签内容的影片剪辑。
		 */
		public function TabbedPane (content : DisplayObjectContainer = null)
		{
			this.content = content;
			this.tabs = new Array( );
			this.selectedIndex = - 1;
			this.group = new ButtonGroup( true );
		}

		/**
		 * 添加一个切换页签
		 * @param btn		切换页签的按钮影片剪辑
		 * @param child		页签切换时要显示的内容
		 */
		public function addTab (btn : MovieClip,child : DisplayObject = null) : void
		{
			//设置按钮行为
			var easyBtn : EasyButton = EasyButton.createEasyButton( btn , true );
			easyBtn.addSelectListener( this.__onBtnSelected );
			easyBtn.addEventListener( MouseEvent.ROLL_OVER , this.__onRollover );
			easyBtn.addEventListener( MouseEvent.ROLL_OUT , this.__onRollout );
			this.group.appendButton( easyBtn );
			this.tabs.push( {btn:easyBtn , child:child} );
		}

		public function destroy():void
		{
			this.content = null;
			group.destroy();
			group = null;
			tabs.length = 0;
		}
		private function __onBtnSelected (e : Event) : void
		{
			var btn : EasyButton = e.target as EasyButton;
			if(btn.isSelected( ))
			{
				for(var i : int = 0; i < this.tabs.length; i ++)
				{
					if(btn == this.tabs[i]["btn"])
					{
						this.setTabIndex( i );
						break;
					}
				}
			}
		}

		private function __onRollover (e : Event) : void
		{
			var btn : EasyButton = e.target as EasyButton;
			if(! btn.isSelected( ))
			{
				btn.parent.setChildIndex( btn , btn.parent.numChildren - 1 );
			}
		}

		private function __onRollout (e : Event) : void
		{
			var btn : EasyButton = e.target as EasyButton;
			if(! btn.isSelected( ) && this.selectedIndex >= 0)
			{
				btn.parent.setChildIndex( btn , btn.parent.getChildIndex( this.tabs[this.selectedIndex]["btn"] ) );
			}
		}

		/**
		 * 切换页签。
		 * @param index			页签的索引。
		 * @throws RangeError	索引错误
		 */
		public function setTabIndex (index : int) : void
		{
			if(index > this.tabs.length - 1 || index < 0)
			{
				throw new RangeError( "索引错误" );
			}
			if(this.selectedIndex == index)return;
			//
			this.group.setAllowNoSelection( false );
			
			var oldBtn : EasyButton = this.selectedIndex >= 0 ? this.tabs[this.selectedIndex]["btn"] : null;
			var oldChild : DisplayObject = this.selectedIndex >= 0 ? this.tabs[this.selectedIndex]["child"] : null;
			var newBtn : EasyButton = this.tabs[index]["btn"];
			var newChild : DisplayObject = this.tabs[index]["child"];
			
			//删除先前的内容
			if(oldBtn)
			{
				oldBtn.setSelected( false );
			}
			if(this.content && oldChild && this.content.contains( oldChild ))
			{
				this.content.removeChild( oldChild );
			}
			
			//添加新内容
			newBtn.setSelected( true );
			//处理页面
			if(this.content && newChild)
			{
				this.content.addChild( newChild );
			}
			
			this.selectedIndex = index;
			//处理
			newBtn.parent.setChildIndex( newBtn , newBtn.parent.numChildren - 1 );
			this.dispatchEvent( new Event( TAB_CHANGED ) );
		}

		public function unselectedAll () : void
		{
			if(this.selectedIndex >= 0)
			{
				this.group.setAllowNoSelection( true );
				this.tabs[this.selectedIndex]["btn"].setSelected( false );
				this.selectedIndex = - 1;
			}
		}

		/**
		 * 设置tab切换
		 */
		public function setTabEnable (index : int,enabled : Boolean) : void
		{
			if(index > this.tabs.length - 1 || index < 0)
			{
				throw new RangeError( "索引错误" );
			}
			var btn : EasyButton = this.tabs[index]["btn"];
			btn.setEnabled( enabled );
		}
		
		/**
		 * tab按钮
		 */ 
		public function getTabBtn(index:int):EasyButton
		{
			var btn : EasyButton = this.tabs[index]["btn"];
			return btn;
		}

		public function setTabVisible (index : int,visible : Boolean) : void
		{
			if(index > this.tabs.length - 1 || index < 0)
			{
				throw new RangeError( "索引错误" );
			}
			var btn : EasyButton = this.tabs[index]["btn"];
			btn.visible = visible;
		}

		/**
		 * 返回当前选中的索引。
		 * @return		当前选中的索引。
		 */
		public function getSelectedIndex () : int
		{
			return this.selectedIndex;
		}

		/**
		 * 返回当前选中显示的页签。
		 */
		public function getSelectedView () : DisplayObject
		{
			return this.tabs[this.selectedIndex]["child"];
		}

		/**
		 * 返回界面
		 * @param index		界面索引。
		 * @return			界面影片剪辑
		 */
		public function getViewAt (index : int) : DisplayObject
		{
			if(index > this.tabs.length - 1 || index < 0)
			{
				throw new RangeError( "索引错误" );
			}
			return this.tabs[index]["child"];
		}

		/**
		 * 添加改变侦听器。
		 */
		public function addChangeListener (listener : Function,priority : int = 0,useWeakReference : Boolean = false) : void
		{
			this.addEventListener( TAB_CHANGED , listener , false , priority , useWeakReference );
		}

		/**
		 * 移除改变侦听器。
		 */
		public function removeChangeListener (listener : Function) : void
		{
			this.removeEventListener( TAB_CHANGED , listener );
		}
	}
}